package com.example.administrator.sunstart_library;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import SunStarUtils.ChangeModeController;
import SunStarUtils.GlideCircleTransform;
import SunStarUtils.StatusBarCompat;
import SunStarUtils.UtilBox;
import SunStarView.RecycleViewDivider;
import butterknife.ButterKnife;

public abstract class BaseActivity extends AppCompatActivity {
    public static int ROUND_MORE = 0x00000001;
    public static int NORMAL_BACK = 0x00000002;
    public static int NORMAL_MORE = 0x00000003;
    public static int ROUND_BACK = 0x00000004;

    private ImageView back, more;
    private FrameLayout fatherView;
    private TextView titleText;
    private RelativeLayout title,activityBase;
    private View dividerLine;
    private TextView moreText;
    private PopupWindow window;
    private PopupWindow windowShare;
    private View upView;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        // 设置contentFeature,可使用切换动画
        setContentView(R.layout.activity_base);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);//默认不弹出键盘
        Log.d("BaseActivity", this.getClass().getName());
        //注入界面文件
        initView();
        SetStatusBarColor(R.color.textBlcak);
    }

    public void moreListener() {
        showMorePop();
    }

    public void showMorePop() {
        window = new PopupWindow();
        View windowView = View.inflate(this, R.layout.poplistview, null);
        ListView lv = (ListView) windowView.findViewById(R.id.poplistview);
        List<MenuPopBean> popBeanList = new ArrayList<>();
        popBeanList.add(new MenuPopBean(R.mipmap.xiaoxi, "消息"));
        popBeanList.add(new MenuPopBean(R.mipmap.home_menu, "首页"));
        popBeanList.add(new MenuPopBean(R.mipmap.fenxiang_white, "分享"));
        popBeanList.add(new MenuPopBean(R.mipmap.fankui, "反馈意见"));
        popBeanList.add(new MenuPopBean(R.mipmap.hezuo, "合作加盟"));
        pop_list_adapter adapter = new pop_list_adapter(popBeanList, this);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                switch (i) {
                    case 0:
                        onmessage();
                        window.dismiss();
                        break;
                    case 1:
                        onmainactivity();
                        window.dismiss();
                        break;
                    case 2:
                        window.dismiss();
                        upView.setVisibility(View.VISIBLE);
                        showSharePop();
                        break;
                    case 3:
                        onsuggest();
                        window.dismiss();
                        break;
                    case 4:
                        onenjoy();
                        window.dismiss();
                        break;
                }


            }
        });
        window.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        window.setWidth(UtilBox.getWindowWith(this) / 3);
        ColorDrawable drawable = new ColorDrawable();
        drawable.setColor(getResources().getColor(R.color.nocolor));
        window.setBackgroundDrawable(drawable);
        window.setContentView(windowView);
        window.setOutsideTouchable(true);
        window.setFocusable(true);
        window.showAsDropDown(title, UtilBox.getWindowWith(this),0,Gravity.RIGHT);

    }

    public void onmessage() {

    }

    public void onmainactivity() {
    }

    public void onsuggest() {
    }

    public void onenjoy() {
    }

    public void showSharePop() {
        upView.setVisibility(View.VISIBLE);
        windowShare = new PopupWindow(BaseActivity.this);
        View shareWindowView = View.inflate(this, R.layout.share_pop_window_view, null);
        RecyclerView recyclerView = (RecyclerView) shareWindowView.findViewById(R.id.base_share_window_rc);
        TextView dismiss = (TextView) shareWindowView.findViewById(R.id.base_share_window_disimiss);
        dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                windowShare.dismiss();
                upView.setVisibility(View.GONE);
            }
        });
        List<MenuPopBean> popBeanList = new ArrayList<>();
        popBeanList.add(new MenuPopBean(R.mipmap.weixin, "微信好友"));
        popBeanList.add(new MenuPopBean(R.mipmap.pengyouquan, "朋友圈"));
        popBeanList.add(new MenuPopBean(R.mipmap.qq, "QQ好友"));
        popBeanList.add(new MenuPopBean(R.mipmap.kongjian, "QQ空间"));
        popBeanList.add(new MenuPopBean(R.mipmap.weibo, "新浪微博"));
        popBeanList.add(new MenuPopBean(R.mipmap.lianjie, "复制链接"));
        SharePopAdapter adapter = new SharePopAdapter(this, popBeanList);
        GridLayoutManager manager = new GridLayoutManager(this, 3);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(manager);
        recyclerView.addItemDecoration(new RecycleViewDivider(this,LinearLayoutManager.VERTICAL,40,R.color.basebg));
        windowShare.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        windowShare.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        ColorDrawable drawable = new ColorDrawable();
        drawable.setColor(getResources().getColor(R.color.nocolor));
        windowShare.setBackgroundDrawable(drawable);
        windowShare.setContentView(shareWindowView);
        windowShare.setInputMethodMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        windowShare.setAnimationStyle(R.style.show_pop_ani);
        windowShare.showAtLocation(activityBase, Gravity.BOTTOM, 0, 0);
    }

    public void backListener() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        // 隐藏软键盘
        imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        UtilBox.dismissDialog();
        ButterKnife.reset(this);

    }

    private void initView() {
        activityBase = (RelativeLayout) findViewById(R.id.activity_base);
        upView = findViewById(R.id.base_activity_upview);
        moreText = (TextView) findViewById(R.id.base_activity_moretext);
        back = (ImageView) findViewById(R.id.base_activity_back);
        dividerLine = findViewById(R.id.base_activity_divider_line);
        fatherView = (FrameLayout) findViewById(R.id.base_activity_father_view);
        more = (ImageView) findViewById(R.id.base_activity_more);
        title = (RelativeLayout) findViewById(R.id.base_activity_title);
        titleText = (TextView) findViewById(R.id.base_activity_title_text);
        fatherView.addView(View.inflate(this, setBaseView(), null));
        if (showTitle() == false) {
            dividerLine.setVisibility(View.GONE);
            title.setVisibility(View.GONE);
        } else {
            dividerLine.setVisibility(View.VISIBLE);
            title.setVisibility(View.VISIBLE);
        }
        if (showMore() == true) {
            more.setVisibility(View.VISIBLE);
        } else {
            more.setVisibility(View.GONE);
        }
        titleText.setText(setTitleText());
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                backListener();
            }
        });
        more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                moreListener();
            }
        });
        moreText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                moretextListener();
            }
        });
        upView.setVisibility(View.GONE);
        upView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                windowShare.dismiss();
                upView.setVisibility(View.GONE);
            }
        });
    }


    public void moretextListener() {
        Toast.makeText(this, "更多", Toast.LENGTH_SHORT).show();
    }

    public void showMoreTextVisibility(boolean b) {
        if (b == true) {
            moreText.setVisibility(View.VISIBLE);
        } else {
            moreText.setVisibility(View.GONE);
        }
        if (more.getVisibility() == View.GONE) {
            moreText.setPadding(20, 20, 20, 20);
        }
    }



    public void setMoreText(String text) {
        moreText.setText(text);
    }

    /**
     * 设置界面主体
     *
     * @return
     */
    public abstract int setBaseView();

    /**
     * 设置标题是否显示
     *
     * @return
     */
    public abstract boolean showTitle();

    /**
     * 设置标题文字
     *
     * @return
     */
    public abstract String setTitleText();

    /**
     * 设置更多是否显示
     *
     * @return
     */
    public abstract boolean showMore();

    /**
     * 显示更多类型圆形或者正常
     *
     * @param type
     * @param Id   图片资源文件
     */
    public void setMore(int type, int Id) {
        if (type == ROUND_MORE) {
            Glide.with(this).load(Id).transform(new GlideCircleTransform(this)).into(more);
        } else {
            Glide.with(this).load(Id).into(more);
        }
    }

    /**
     * 设置返回类型圆形或者正常
     *
     * @param type
     * @param Id   图片资源文件
     */
    public void setBack(int type, int Id) {
        if (type == NORMAL_BACK) {
            Glide.with(this).load(Id).transform(new GlideCircleTransform(this)).into(back);
        } else {
            Glide.with(this).load(Id).into(back);
        }
    }

    /**
     * 设置主体背景颜色
     *
     * @param baseColor
     */
    public void setBaseBack(String baseColor) {
        fatherView.setBackgroundColor(Color.parseColor(baseColor));
    }

    /**
     * 设置是否显示返回
     *
     * @param showBack
     */
    public void showBack(boolean showBack) {
        if (showBack == true)
            back.setVisibility(View.VISIBLE);
        else
            back.setVisibility(View.GONE);
    }

    /**
     * 利用16进制色值设置顶部标题背景颜色
     *
     * @param titleColor
     */
    public void setTitleBack(String titleColor) {
        title.setBackgroundColor(Color.parseColor(titleColor));
    }

    /**
     * 设置顶部标题背景图片
     * 会覆盖背景颜色
     *
     * @param id
     */
    public void setTitleBackImg(int id) {
        title.setBackgroundResource(id);
    }

    /**
     * 利用资源Id设置顶部标题背景颜色
     *
     * @param id
     */
    public void setTitleBack(int id) {
        title.setBackgroundColor(id);
    }

    /**
     * 界面跳转
     *
     * @param cls
     */
    public void goToActivity(Class<?> cls) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
    }

    /**
     * 界面跳转并kill掉本页
     *
     * @param cls
     */
    public void goToActivityFinish(Class<?> cls) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
        finish();
    }

    /**
     * 根据资源文件设置title文字颜色
     *
     * @param id
     */
    public void setTitleTextColor(int id) {
        titleText.setTextColor(getResources().getColor(id));
    }

    /**
     * 根据16进制色值设置title文字颜色
     *
     * @param colorString
     */
    public void setTitleTextColor(String colorString) {
        titleText.setTextColor(Color.parseColor(colorString));
    }

    /**
     * 设置主题
     */
    private void initTheme() {
        ChangeModeController.setTheme(this, R.style.DayTheme, R.style.NightTheme);
    }

    /**
     * 着色状态栏（4.4以上系统有效）
     */
    protected void SetStatusBarColor() {
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.main_color));
    }

    /**
     * 着色状态栏（4.4以上系统有效）
     */
    public void SetStatusBarColor(int color) {
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, color));
    }



    /**
     * 沉浸状态栏（4.4以上系统有效）
     */
    protected void SetTranslanteBar() {
        StatusBarCompat.translucentStatusBar(this);
    }

    @Override
    public void onBackPressed() {
        if (window != null && window.isShowing()) {
            window.dismiss();
        } else if (windowShare != null && windowShare.isShowing()) {
            upView.setVisibility(View.GONE);
            windowShare.dismiss();
        } else {
            super.onBackPressed();
        }
    }
}

